/*
 * Copyright (C) 2012-2013  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.swing.internal;

import java.awt.Container;
import java.util.Map;

import javax.swing.JLabel;

import nemos.swing.IComponentFactory;
import nemos.swing.util.ResourceComponentFactory;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.ConfigurationPolicy;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.cm.Configuration;
import org.osgi.service.component.ComponentContext;

/**
 * ConfigurableComponentFactory : The configurable component factory.
 * 
 * <p>
 * One instance of this class is registered as a {@link IComponentFactory} for
 * each {@link Configuration} registered under {@value #CONFIGURATION_NAME}.
 * </p>
 * 
 * @author Romain DUBOIS
 */
@Properties({
        @Property(name = ConfigurableComponentFactory.RESOURCE_BASE_NAME, value = "components"),
        @Property(name = ConfigurableComponentFactory.RESOURCE_NAME, cardinality = 1) })
@Service(IComponentFactory.class)
@Component(name = ConfigurableComponentFactory.CONFIGURATION_NAME, policy = ConfigurationPolicy.REQUIRE)
public class ConfigurableComponentFactory extends ResourceComponentFactory<Container> {

    /** The configuration name */
    static final String CONFIGURATION_NAME = "nemos.swing.component.generic"; //$NON-NLS-1$

    /** The resource base name property */
    static final String RESOURCE_BASE_NAME = "baseName"; //$NON-NLS-1$
    /** The resource name property */
    static final String RESOURCE_NAME = "name"; //$NON-NLS-1$

    /** The resource base name */
    private String mResourceBaseName;
    /** The resource name */
    private String mResourceName;

    /**
     * Constructor
     */
    public ConfigurableComponentFactory() {
        super(Container.class);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.util.ResourceComponentFactory#start(org.osgi.service.component.ComponentContext)
     */
    @Override
    public void start(final ComponentContext pContext) {
        super.start(pContext);

        mResourceBaseName = (String) pContext.getProperties().get(RESOURCE_BASE_NAME);
        mResourceName = (String) pContext.getProperties().get(RESOURCE_NAME);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.util.ResourceComponentFactory#createDefault(java.lang.Throwable,
     *      java.lang.String, java.lang.Object[])
     */
    @Override
    protected Container createDefault(final Throwable pException, final String pMessageCode,
            final Object... pParams) {

        // Log the error
        getLogger().error(pMessageCode, pException, pParams);

        // Create a simple label
        return new JLabel(getText(true, pMessageCode, pParams));
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.util.ResourceComponentFactory#getResourceName(java.util.Map)
     */
    @Override
    protected String getResourceName(@SuppressWarnings("unused") final Map<String, ?> pInitParams) {
        return mResourceName;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.util.ResourceComponentFactory#getResourceBaseName(java.util.Map)
     */
    @Override
    protected String getResourceBaseName(
            @SuppressWarnings("unused") final Map<String, ?> pInitParams) {
        return mResourceBaseName;
    }
}
